<?php
include_once("common.php");

$id = $_REQUEST['event_nid'];

$sql = "update ".$table_Prefix."event set `view` = `view`+1 where  nid =".$id;
do_query($sql);


if($user_id > 0){
    $view_info = select_one_line_all_items($table_Prefix."event_view_log",'event_id='.$id.' and user_id ='.$user_id);
    if(!$view_info){
        $view_insert['ai_event_id'] = $id;
        $view_insert['ai_user_id'] = $user_id;
        $insert_sql = insert_date($table_Prefix."event_view_log",$view_insert);
        do_query($insert_sql);
    }
}


$detail = select_one_line_all_items($table_Prefix."event",'nid='.$id);

//参加活动的用户
$member_list = select_lines_all_items($table_Prefix."event_member",'nid desc' , 'is_delete = 0 and event_nid ='.$id);

//标签
$tag = select_one_line_all_items($table_Prefix."member_setting",' nid=1');
$tagArr = explode(',', str_replace('，',',',$tag['tag']));

$member_new_list = array();
foreach ($member_list as $member_key=>$member_v){

    $member = select_one_line_all_items($table_Prefix."member",' nid='.$member_v['user_id']);

    $tagList=select_lines_all_items($table_prefix."member_tag","","leader_nid=".$member_v['user_id'],"");

    $data =  array();
    foreach ($tagArr as $key => $tagV){
        $data[$key]['tag'] = $tagV;
        $data[$key]['num'] = 0;
        $data[$key]['checked'] = false;

        foreach ($tagList as $keyL => $tagL){
            if(strstr($tagL['tag'],$tagV)){
                $data[$key]['num']++;
                if($user_id == $tagL['member_nid']){
                    $data[$key]['checked'] = true;
                }
            }
        }
    }

    //自定义类型 用于标签的颜色变化
    $tag_list = array();
    $tag_type = ['success', 'primary', 'warning', 'error'];

    //按点赞数 desc
    $all_num = array_column($data,'num');
    array_multisort($all_num,SORT_DESC,$data);

    // 随机
//    foreach (array_slice($data,mt_rand(0,count($data) - 3),3) as $nkey => $nvalue){
//        $nvalue['type'] = $tag_type[$nkey];
//        $tag_list[$nkey] = $nvalue;
//    }
        $be_tag_data = array_slice($data,0,3);
        //获取数组前三个
        foreach ($be_tag_data as $nkey => $nvalue){

            //没有点赞的标签 随机拿一个
            if($nvalue['num'] == 0){
                $nvalue = $data[mt_rand($nkey,count($data) - $nkey)];
            }
            //设置默认颜色
            $nvalue['type'] = $tag_type[$nkey];
            $tag_list[$nkey] = $nvalue;

    }


    $member_new =  array();
    $member_new['nid'] = $member['nid'];
    $member_new['nickname'] = $member['nickname'];
    $member_new['headimgurl'] = $member['headimgurl'];
    //点赞人数
    $member_new['like_num'] = count_by_item( "athlon_member_like",' is_delete = 0 AND event_nid = '.$id.' AND like_member_nid ='.$member['nid']);
    $member_new['is_like'] = count_by_item( "athlon_member_like",' is_delete = 0 AND event_nid = '.$id.' AND member_nid ='.$user_id.' AND like_member_nid ='.$member['nid']) > 0 ? true : false;

    $member_new['tag_list'] = $tag_list;



    $member_new_list[$member_key] = $member_new;
}

//感兴趣
$view_log = select_lines_all_items($table_Prefix."event_view_log",'nid desc' , 'event_id ='.$id);

//局长
$leader = select_one_line_all_items($table_Prefix."member",' nid='.$detail['employee_id']);
$leader_obj['name'] = $leader['nickname'];
$leader_obj['headimgurl'] = $leader['headimgurl'];
$leader_obj['nid'] = $leader['nid'];

$re_data = array();
$re_data['nid'] = $detail['nid'];
$re_data['theme'] = $detail['theme'];
$re_data['image1']  = $detail['image1'] ? domain_img() . $detail['image1'] : '';
$re_data['view']               = $detail['view'];
$re_data['interested']         = count($view_log);

$re_data['content'] =  $detail['finnsh_content'];
$re_data['leader'] = $leader_obj;
$re_data['member_list'] = $member_new_list;
$re_data['over_content'] = $detail['over_content'];
//图片
$file_list = select_lines_all_items($table_Prefix."files",'nid asc' , 'obj_nid ='.$detail['nid']." and type='event'");
$file_arr = array();
foreach ($file_list as $fKey => $fValue){
    array_push($file_arr,$fValue['url']);
}

$re_data['file_list'] = $file_arr;

$arr['status'] = 200;
$arr['info'] = $re_data;
die(json_encode($arr)) ;
